﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace ODM_Code.Forms
{
    public partial class AlternativeSetupForm : Form
    {
        private Code.AlternativeSetupCode asc;
        private string dbfPath;
        private string inputPath;

        public AlternativeSetupForm()
        {
            InitializeComponent();
            asc = new Code.AlternativeSetupCode();
            dbfPath = "C:\\magfire\\" + ConfigSettings.ReadSetting("analysisArea");
            inputPath = dbfPath + "\\input";
        }

        private void AlternativeSetupForm_Load(object sender, EventArgs e)
        {
            this.updateAlternativeList();
            label1.Text += ConfigSettings.ReadSetting("analysisArea");
        }

        private void butNew_Click(object sender, EventArgs e)
        {
            this.Visible = false;

            NewAlternativeForm naf = new NewAlternativeForm();
            naf.Activate();
            naf.ShowDialog();
            naf.BringToFront();

            this.updateAlternativeList();
            this.Visible = true;
        }

        private void butDelete_Click(object sender, EventArgs e)
        {

        }

        private void butDone_Click(object sender, EventArgs e)
        {
            string selectedAlternative = lbExistingAlternatives.SelectedItem.ToString();
            ConfigSettings.WriteSetting("currentAlternative", selectedAlternative);
            this.Close();
        }

        private void butLaunch_Click(object sender, EventArgs e)
        {
            string alternativepath = inputPath + "\\" + lbExistingAlternatives.SelectedItem.ToString();
            string intensity = this.getIntensity();
            string clusterSize = tbClusterSize.Text;
            int numFires = asc.CountFires(alternativepath);

            asc.WriteParametersFile(alternativepath, null, intensity, clusterSize, numFires);

            Process solver = new Process();
            solver.StartInfo.FileName = "C:\\magfire\\main\\FireMAGIS.exe";
            solver.StartInfo.WorkingDirectory = "C:\\magfire\\main";
            solver.Start();
            
            this.Close();
        }

        private string getIntensity()
        {
            RadioButton[] rbs = { rbSingleIteration, rbLowIntensity, rbMediumIntensity, rbHighIntensity };
            string intensity = "";
            foreach (RadioButton rb in rbs)
            {
                if (rb.Checked)
                {
                    string text = rb.Text;
                    intensity = text.Substring(text.IndexOf('(') + 1, 1);
                }
            }

            return intensity;
        }

        private void updateAlternativeList()
        {
            lbExistingAlternatives.Items.Clear();
            List<string> existingAlternatives = asc.GetExistingAlternatives(inputPath);
            foreach (string s in existingAlternatives)
            {
                lbExistingAlternatives.Items.Add(s);
            }
        }
    }
}
